package com.zsgeek.wisdchain.qutu.base.event;

import android.content.Context;
import android.content.SharedPreferences;

import com.zsgeek.wisdchain.qutu.QuTuApplication;
import com.zsgeek.wisdchain.qutu.tools.UrlPathHelper;

/**
 * @author 王亨彬
 * @date 2018/10/7
 * email：793325917@qq.com
 */
public class PreferenceHelper {
    private static SharedPreferences mSharedPreferences;
    private static PreferenceHelper instance;
    private static SharedPreferences.Editor editor;

    private static String SHARED_KEY_LOGIN_LAST_USERID = "CUA_LOGIN_LAST_USERID";    //登录后的userid
    private static String SHARED_KEY_LOGIN_LAST_SESSIONID = "CUA_LOGIN_LAST_SESSIONID";    //登录后的userid
    private static String SHARED_KEY_LOGIN_LAST_USERNAME = "CUA_LOGIN_LAST_USERNAME";    //登录的用户名
    private static String SHARED_KEY_LOGIN_LAST_PASSWORD = "CUA_LOGIN_LAST_PASSWORD";    //登录的密码
    //    private static String SHARED_KEY_TRADE_LOGIN_USERNAME = "CUA_TRADE_LOGIN_USERNAME";    //交易登录的用户名
    private static String SHARED_KEY_LOGIN_LAST_TYPE = "CUA_LOGIN_LAST_TYPE";    //登录的类型
    private static String SHARED_KEY_LOGIN_LAST_TOKEN = "CUA_LOGIN_LAST_TOKEN";    //第三方登录的token
    private static String SHARED_KEY_LOGIN_LAST_OPENID = "CUA_LOGIN_LAST_OPENID";    //第三方登录的
    private static String SHARED_KEY_LOGIN_LAST_PLATFORM = "CUA_LOGIN_LAST_PLATFORM";    //登录的类型
    private static String SHARED_KEY_LOGIN_LAST_ACCOUNT = "CUA_LOGIN_LAST_ACCOUNT";    //登录的账号信息
    private static String CUA_KEY_LOGINDATABEAN = "CUA_LOGIN_DATA_BEAN";    //登录保存状态
    public static final String GESTURE_FLG = "gesture_flg"; // 判断是否设置有手势密码
    public static final String GESTURE_TIME = "gesture_time"; // 手势密码输入错误超过5次时间
    public static final String FINGEER_FLG = "finger_flg";//存储指纹flg

//    public static String BroadCastParameterEVENT_Cropu_picter = "0x801";
//    public static String EVENT_BUY_SELL_SEVEN_SOCKET_SERVICE = "0x802";
//    public static String EVENT_WEBSOCKET_HIGH_LOW_NEW = "0x803";


    private PreferenceHelper(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences("WDC_WalletPreferenceHelper", Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static PreferenceHelper getInstance() {
        if (instance == null) {
            synchronized (PreferenceHelper.class) {
                if (instance == null) {
                    instance = new PreferenceHelper(QuTuApplication.getInstance());
                }
            }
        }

        return instance;
    }

    public String getLastUserId() {
        return mSharedPreferences.getString(SHARED_KEY_LOGIN_LAST_USERID, null);
    }

    public SharedPreferences getSharedPreferences() {
        return mSharedPreferences;
    }

    public SharedPreferences.Editor getEditor() {
        return editor;
    }

    /**
     * 保存登录成功后的userId
     *
     * @param userId
     */
    public void saveLastUserId(String userId) {
        editor.putString(SHARED_KEY_LOGIN_LAST_USERID, userId);
        editor.commit();
    }

    public void saveSessionId(String userId) {
        editor.putString(SHARED_KEY_LOGIN_LAST_SESSIONID, userId);
        editor.commit();
    }

    public String getSessionId() {
        return mSharedPreferences.getString(SHARED_KEY_LOGIN_LAST_SESSIONID, null);
    }

    /**
     * 保存登录的userName
     *
     * @param userName
     */
    public void saveLastUserName(String userName) {
        editor.putString(SHARED_KEY_LOGIN_LAST_USERNAME, userName);
        editor.commit();
    }

    public String getLastUserName(String defaultValue) {
        return mSharedPreferences.getString(SHARED_KEY_LOGIN_LAST_USERNAME, defaultValue);
    }

    /**
     * 保存首次安装记录
     *
     * @param defaultValue 默认false
     */
    public void setFirstInstallation(boolean defaultValue) {
        editor.putBoolean(UrlPathHelper.PackageNames + "/FirstInstallation", defaultValue);
        editor.commit();
    }

    public boolean getFirstInstallation(boolean defaultValue) {
        return mSharedPreferences.getBoolean(UrlPathHelper.PackageNames + "/FirstInstallation", defaultValue);
    }

    /**
     * 保存登录的密码
     *
     * @param password
     */
    public void saveLastPassword(String password) {
        if (password == null) {
            editor.remove(SHARED_KEY_LOGIN_LAST_PASSWORD);
        } else {
            editor.putString(SHARED_KEY_LOGIN_LAST_PASSWORD, password);
        }
        editor.commit();
    }

    /**
     * 保存最后登录的账号信息
     *
     * @param nick
     */
    public void saveLastAccount(String nick) {
        if (nick == null) {
            editor.remove(SHARED_KEY_LOGIN_LAST_ACCOUNT);
        } else {
            editor.putString(SHARED_KEY_LOGIN_LAST_ACCOUNT, nick);
        }
        editor.commit();
    }

    /**
     * 保存登录的类型
     *
     * @param type
     */
    public void saveLastLoginType(int type) {
        editor.putInt(SHARED_KEY_LOGIN_LAST_TYPE, type);
        editor.commit();
    }

    /**
     * 第三方登录的类型（QQ、微信、微博）
     *
     * @param platform
     */
    public void saveLastPlatform(int platform) {
        editor.putInt(SHARED_KEY_LOGIN_LAST_PLATFORM, platform);
        editor.commit();
    }

    /**
     * 第三方登录的token
     *
     * @param token
     */
    public void saveLastToken(String token) {
        if (token == null) {
            editor.remove(SHARED_KEY_LOGIN_LAST_TOKEN);
        } else {
            editor.putString(SHARED_KEY_LOGIN_LAST_TOKEN, token);
        }
        editor.commit();
    }

    /**
     * 第三方登录的uuid
     *
     * @param openId
     */
    public void saveLastOpenId(String openId) {
        if (openId == null) {
            editor.remove(SHARED_KEY_LOGIN_LAST_OPENID);
        } else {
            editor.putString(SHARED_KEY_LOGIN_LAST_OPENID, openId);
        }
        editor.commit();
    }

    /**
     * 保存登录对象
     *
     * @param strObject 序列化对象
     */
    public void saveLoginDataBean(String strObject) {
        if (strObject == null) {
            editor.remove(CUA_KEY_LOGINDATABEAN);
        } else {
            editor.putString(CUA_KEY_LOGINDATABEAN, strObject);
        }
        editor.commit();
    }

//    /**
//     * 取登录对象
//     *
//     * @param defaultValue 反序列化对象
//     */
//    public String getLoginDataBean(String defaultValue) {
//        return mSharedPreferences.getString(CUA_KEY_LOGINDATABEAN, defaultValue);
//    }

//    /**
//     * 序列化对象
//     *
//     * @param person
//     * @return
//     * @throws IOException
//     */
//    public String serialize(Object person) throws Exception {
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
//        objectOutputStream.writeObject(person);
//        String serStr = byteArrayOutputStream.toString("ISO-8859-1");
//        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
//        objectOutputStream.close();
//        byteArrayOutputStream.close();
//        return serStr;
//    }

//    /**
//     * 反序列化对象
//     *
//     * @param str
//     * @return
//     * @throws IOException
//     * @throws ClassNotFoundException
//     */
//    public Object deSerialization(String str) throws Exception {
//        if (TextUtils.isEmpty(str))
//            return null;
//        String redStr = java.net.URLDecoder.decode(str, "UTF-8");
//        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes(StandardCharsets.ISO_8859_1));
//        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
//        Object person = objectInputStream.readObject();
//        objectInputStream.close();
//        byteArrayInputStream.close();
//        return person;
//    }

    public static void putGestureFlag(boolean flg) {
        editor.putBoolean(GESTURE_FLG, flg);
        editor.commit();
    }

    public static boolean getGestureFlag() {
        return mSharedPreferences.getBoolean(GESTURE_FLG, false);
    }

    public static void putGestureTime(long time) {
        editor.putLong(GESTURE_TIME, time);
        editor.commit();
    }

    public static long getGestureTime() {
        return mSharedPreferences.getLong(GESTURE_TIME, 0);
    }

    public static void putFingerFlg(boolean flg) {
        editor.putBoolean(FINGEER_FLG, flg);
        editor.commit();
    }

    public static boolean getFingerFlg() {
        return mSharedPreferences.getBoolean(FINGEER_FLG, false);
    }
}
